String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}

function init_topics($tree, prefix)
{
	if (prefix == null || prefix == undefined) 
		prefix = '';
	$tree.find('li.node').each(
		function(index)
		{
			var $this = $(this);
			var node_id = eval("this.id.replace (/^"+prefix+"node_/, '')");
			var $children = $this.children('ul').children('li.node');
			
			// catch adviser parent checkbox state
			$this.bind('validation', function(event, type, checked, id){
				var $cbox = $("#"+prefix+type+"_"+node_id);
				var $input = $("#"+prefix+type+"_validation_"+node_id);
				var toTrigger = false;
				
				if (checked){
					if (!$cbox.is(':checked')) {
						// automatically check the checkbox, make it read only
						// and change adviser_validation item
						$cbox.attr('checked', true);
						$cbox.attr("disabled", true);
						$input.val(id);
						toTrigger = true;
					}
				}else{
					if ($cbox.is(':checked') && $input.val() == id) {
						// automatically uncheck the checkbox if 
						// it was checked by the unchecked item
						$cbox.attr('checked', false);
						$cbox.attr("disabled", false);
						$input.val("");
						toTrigger = true;
					}
				}
				
				if (toTrigger)
				{
					$children.trigger('validation', [type, checked, id]);
				}
				event.preventDefault();
				return false;
			});

			// when adviser checkbox change, we notify all children
			$("#"+prefix+"adviser_"+node_id).click(function(event){
				$children.trigger('validation', ['adviser', $(this).is(':checked'), node_id]);
			});
			$("#"+prefix+"owner_"+node_id).click(function(event){
				$children.trigger('validation', ['owner', $(this).is(':checked'), node_id]);
			});
			$("#"+prefix+"comment_layer_"+node_id).each(function(index){
				var $layer = $(this);
				var $img = $layer.siblings('img');
				$img.toggle(function(){
					$layer.hide();
					this.src = 'images/arrow_down_blue.png';
				}, function(){
					$layer.show();
					$layer.css("height", 200);
					this.src = 'images/arrow_up_blue.png';
				}).siblings('a[href="#"]').toggle(function(event){
					if (event.detail == 1){
						$img.click();
					}
					return true;
				}, function(event){
					if (event.detail == 1){
						$img.click();
					}
					return true;
				}).end().click();


				$this.bind('toggle_comment', function(event){ 
					$img.click();
					return true;
				}).bind('show_comment', function(event){ 
					if (!$layer.is(":visible")) 
						$img.click();
					return true;
				}).bind('hide_comment', function(event){ 
					if ($layer.is(":visible")) 
						$img.click(); 
					return true;
				}).bind('show_image', function(event){ 
					$img.show(); 
					return true;
				}).bind('hide_image', function(event){ 
					if ($layer.is(":visible")) 
						$img.click(); 
					$img.hide(); 
					return true;
				});
				
			});
		}
	);
}



